<?php

/**
 * @package nuo
 *
 */
class Nuo_Controller_Action_Block
{

	// Properties

	/**
	 * @property Zend_View
	 */
	public $view;

	/**
	 * action ane
	 * @property string
	 */
	protected $_action;

	/**
	 * content result
	 */
	protected $_content;

	/**
	 * @property bool
	 */
	protected $_isNoRender = false;

	/**
	 * @property request
	 */
	protected $_request;

	/**
	 * script path to this object
	 * @property script path
	 */
	protected $_scriptPath;

	// General

	public function setContent($content)
	{
		$this -> _content = $content;
		return $this;
	}

	public function appendContent($content)
	{
		$this -> _content .= $content;
		return $this;
	}

	public function getContent()
	{
		return $this -> _content;
	}

	/**
	 * set is no render
	 * @property bool $flag
	 * @return Nuo_Controller_Action_Widget
	 */
	public function setNoRender($flag = true)
	{
		$this -> _noRender = (bool)$flag;
		return $this;
	}

	/**
	 * check if is no render mode
	 */
	public function isNoRender()
	{
		return $this -> _isNoRender;
	}

	/**
	 * @param  Zend_Controller_Request_Abstract $request
	 * @return Nuo_Controller_Action_Widget
	 */
	public function setRequest(Zend_Controller_Request_Abstract $request)
	{
		$this -> _request = $request;
		return $this;
	}

	/**
	 * get current request to this instance
	 * @return Zend_Controller_Request_Abstract
	 */
	public function getRequest()
	{
		return $this -> _request;
	}

	/**
	 * @param string $script path
	 * @return Nuo_Controller_Action_Widget
	 */
	public function setScriptPath($script_path)
	{
		$this -> _scriptPath = $script_path;
		return $this;
	}

	/**
	 * @return string
	 */
	public function getScriptPath()
	{
		if (null == $this -> _scriptPath)
		{
			$cls = get_class($this);
			$arr = explode('_', str_replace('_Controller', '', get_class($this)));

			$m = array_shift($arr);
			array_shift($arr);
			$c =  array_shift($arr);
			$this -> _scriptPath = APPLICATION_PATH . '/modules/' . $m .'/views/blocks/' . Nuo::deflect($c);
		}

		return $this -> _scriptPath;
	}

	/**
	 * @return Zend_View
	 */
	public function getView()
	{
		return Zend_Registry::get('Zend_View');
	}

	/**
	 * @param string $action
	 * @return string
	 */

	public function dispatch($action = null)
	{
		// Begin generation
		$result = '';

		if (empty($action))
		{
			$action = 'index';
		}

		try
		{
			ob_start();

			$this -> _action = $action;

			$method = $this -> inflect($action) . 'Action';

			if (!method_exists($this, $method))
			{
				throw new Nuo_Exception(sprintf('Action "%s" does not exist in widget "%s"', $action, get_class($this)));
			}

			// Prepare stuff
			$view = $this -> getView();
			$old = $view -> getVars();
			$view -> clearVars();
			$this -> view = $view;

			// dispatch action
			$this -> $method();

			// Render
			if (!$this -> isNoRender())
			{
				$path = $this -> getScriptPath() . '/' . $this -> _action . '.phtml';
				$result = $view -> renderScript($path);
			}

			$view -> clearVars();
			$view -> assign($old);

			$result .= ob_get_clean();

		}
		catch( Exception $e )
		{
			$result = ob_end_clean();
			throw $e;
		}

		return $result;
	}

	// Utility

	public function inflect($action)
	{
		$action = str_replace(' ', '', ucwords(str_replace(array(
			'.',
			'-'
		), ' ', $action)));
		return $action;
	}

}
